package com.web.controller;

import com.alibaba.fastjson.JSON;
import com.web.entity.ResponseResult;
import com.web.entity.Student;
import com.web.service.StudentService;
import com.web.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/insertStudent")
public class InsertStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String gender = req.getParameter("gender");
        String cellphone = req.getParameter("cellphone"); // 获取 cellphone 参数
        String address = req.getParameter("address"); // 获取 address 参数

        Student student = new Student();
        student.setName(name);
        student.setAge(age);
        student.setGender(gender);
        student.setCellphone(cellphone); // 设置 cellphone 参数
        student.setAddress(address); // 设置 address 参数

        StudentService studentService = new StudentServiceImpl();
        int rows = studentService.insertStudent(student);
        if (rows > 0) {
            resp.getWriter().write(JSON.toJSONString(new ResponseResult(200, null, "学生插入成功")));
        } else {
            resp.getWriter().write(JSON.toJSONString(new ResponseResult(500, null, "学生插入失败")));
        }
    }
}